﻿//	Author:                 Joe audette
//  Created:			    2010-08-10
//	Last Modified:		    2010-08-10
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web.Framework;
using Resources;

namespace mojoPortal.Web.UI
{
    public class TweetThisLink : WebControl
    {
        
        private const int maxTweetLength = 140;

        private string urlToTweet = string.Empty;

        public string UrlToTweet
        {
            get { return urlToTweet; }
            set { urlToTweet = value; }
        }

        private string titleToTweet = string.Empty;

        public string TitleToTweet
        {
            get { return titleToTweet; }
            set { titleToTweet = value; }
        }

        private string imageUrl = "~/Data/SiteImages/tweetthis3.png";

        public string ImageUrl
        {
            get { return imageUrl; }
            set { imageUrl = value; }
        }

        private bool autoDetectUrlIfNotSpecified = true;

        public bool AutoDetectUrlIfNotSpecified
        {
            get { return autoDetectUrlIfNotSpecified; }
            set { autoDetectUrlIfNotSpecified = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (autoDetectUrlIfNotSpecified)
            {
                if (urlToTweet.Length == 0) { urlToTweet = WebUtils.GetSiteRoot() + Page.Request.RawUrl; }
                if (titleToTweet.Length == 0) { titleToTweet = Page.Title; }
            }

           
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (HttpContext.Current == null)
            {
                writer.Write("[" + this.ID + "]");
                return;
            }

            DoRender(writer);

               
        }



        private void DoRender(HtmlTextWriter writer)
        {
            if (!Visible) { return; }
            if (urlToTweet.Length == 0) { return; }

            string twitterUrl;

            int maxTitleLength = maxTweetLength - (urlToTweet.Length + 1);
            if (maxTitleLength > 0)
            {
                if ((titleToTweet.Length > maxTitleLength)&&(titleToTweet.Length > 3))
                {
                    int len = (maxTitleLength - 3);
                    if ((len > 0)&&(len < titleToTweet.Length))
                    {
                        titleToTweet = titleToTweet.Substring(0, len) + "...";
                    }
                }

                twitterUrl = "http://twitter.com/home?status=" + Page.Server.UrlEncode(titleToTweet + " " + urlToTweet);

            }
            else
            {
                twitterUrl = "http://twitter.com/home?status=" + Page.Server.UrlEncode(urlToTweet);
            }


            if (CssClass.Length == 0) CssClass = "tweetthislink";

            writer.WriteBeginTag("a");
            writer.WriteAttribute("class", CssClass);
            writer.WriteAttribute("title", Resource.TweetThisLink);
            writer.WriteAttribute("href", twitterUrl);
            if (BrowserHelper.IsIE())
            {
                writer.WriteAttribute("target", "_blank");
            }
            else
            {
                writer.WriteAttribute("onclick", "window.open(this.href,'_blank');return false;");
            }

            writer.Write(HtmlTextWriter.TagRightChar);

            writer.WriteBeginTag("img");
            writer.WriteAttribute("src", Page.ResolveUrl(imageUrl));
            writer.WriteAttribute("alt", Resource.TweetThisLink);
            writer.Write(HtmlTextWriter.SelfClosingChars);
            writer.Write(HtmlTextWriter.TagRightChar);
            
            
            writer.WriteEndTag("a");


        }

    }
}